package com.action;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.dao.ProductDAO;
import com.dao.ProductTypeDAO;
import com.dao.impl.ProductDAOImpl;
import com.dao.impl.ProductTypeDAOImpl;
import com.model.ProductType;

/**
 * Servlet implementation class AddProductTypeServlet
 */
public class AddProductTypeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");

		// 获取页面信息
		String id = request.getParameter("id");
		String productTypeName = request.getParameter("productTypeName");
		String description = request.getParameter("description");

		// 添加数据
		ProductType productType = new ProductType();
		productType.setId(Integer.parseInt(id));
		productType.setProductTypeName(productTypeName);
		productType.setDescription(description);

		ProductTypeDAO ptd = new ProductTypeDAOImpl();
		boolean b = ptd.addProductType(productType);

		String resultJson = null;

		if (b) {
			resultJson = "{\"success\":true,\"msg\":\"添加成功！\"}";
		} else {
			resultJson = "{\"success\":false,\"msg\":\"添加失败!\"}";
		}
		response.getWriter().print(resultJson);

	}

}
